<template>
  <div class="admin-layout">
    <el-container class="min-h-screen">
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '240px'" class="sidebar">
        <div class="sidebar-header">
          <div v-if="!isCollapse" class="logo">
            <h2 class="text-white text-lg font-bold">Cursor 学习管理</h2>
          </div>
          <div v-else class="logo-mini">
            <span class="text-white text-xl font-bold">C</span>
          </div>
        </div>
        
        <el-scrollbar class="sidebar-menu-wrapper">
          <el-menu
            :default-active="activeMenu"
            :collapse="isCollapse"
            :unique-opened="true"
            background-color="#304156"
            text-color="#bfcbd9"
            active-text-color="#409EFF"
            router
          >
            <el-menu-item index="/admin/dashboard">
              <el-icon><House /></el-icon>
              <template #title>仪表盘</template>
            </el-menu-item>
            
            <el-sub-menu index="content">
              <template #title>
                <el-icon><Document /></el-icon>
                <span>内容管理</span>
              </template>
              <el-menu-item index="/admin/categories">
                <el-icon><Folder /></el-icon>
                <template #title>分类管理</template>
              </el-menu-item>
              <el-menu-item index="/admin/lessons">
                <el-icon><Reading /></el-icon>
                <template #title>课程管理</template>
              </el-menu-item>
              <el-menu-item index="/admin/tags">
                <el-icon><PriceTag /></el-icon>
                <template #title>标签管理</template>
              </el-menu-item>
              <el-menu-item index="/admin/code-examples">
                <el-icon><DocumentCopy /></el-icon>
                <template #title>代码示例</template>
              </el-menu-item>
            </el-sub-menu>
            
            <el-sub-menu index="docs">
              <template #title>
                <el-icon><Collection /></el-icon>
                <span>文档管理</span>
              </template>
              <el-menu-item index="/admin/docs/categories">
                <el-icon><Files /></el-icon>
                <template #title>文档分类</template>
              </el-menu-item>
              <el-menu-item index="/admin/docs/list">
                <el-icon><Notebook /></el-icon>
                <template #title>文档列表</template>
              </el-menu-item>
              <el-menu-item index="/admin/docs/import">
                <el-icon><Download /></el-icon>
                <template #title>文档导入</template>
              </el-menu-item>
            </el-sub-menu>
            
            <el-menu-item index="/admin/files">
              <el-icon><UploadFilled /></el-icon>
              <template #title>文件管理</template>
            </el-menu-item>
            
            <el-menu-item index="/admin/profile">
              <el-icon><User /></el-icon>
              <template #title>个人资料</template>
            </el-menu-item>
          </el-menu>
        </el-scrollbar>
      </el-aside>
      
      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="navbar">
          <div class="navbar-left">
            <el-button
              type="link"
              @click="toggleSidebar"
              class="collapse-btn"
            >
              <el-icon><Fold v-if="!isCollapse" /><Expand v-else /></el-icon>
            </el-button>
            
            <el-breadcrumb separator="/">
              <el-breadcrumb-item :to="{ path: '/admin/dashboard' }">首页</el-breadcrumb-item>
              <el-breadcrumb-item v-if="currentTitle">{{ currentTitle }}</el-breadcrumb-item>
            </el-breadcrumb>
          </div>
          
          <div class="navbar-right">
            <el-dropdown @command="handleCommand">
              <div class="user-info">
                <el-avatar
                  :src="authStore.user?.avatar"
                  :icon="UserFilled"
                  size="small"
                />
                <span class="username">{{ authStore.user?.username }}</span>
                <el-icon><ArrowDown /></el-icon>
              </div>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="profile">
                    <el-icon><User /></el-icon>
                    个人资料
                  </el-dropdown-item>
                  <el-dropdown-item command="logout" divided>
                    <el-icon><SwitchButton /></el-icon>
                    退出登录
                  </el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </div>
        </el-header>
        
        <!-- 主内容 -->
        <el-main class="main-content">
          <div class="content-wrapper">
            <router-view />
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useAuthStore } from '@/stores/auth'
import {
  House,
  Document,
  Folder,
  Reading,
  PriceTag,
  DocumentCopy,
  UploadFilled,
  User,
  UserFilled,
  Fold,
  Expand,
  ArrowDown,
  SwitchButton,
  Collection,
  Files,
  Notebook,
  Download
} from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const route = useRoute()
const router = useRouter()
const authStore = useAuthStore()

const isCollapse = ref(false)

// 计算当前激活的菜单
const activeMenu = computed(() => route.path)

// 计算当前页面标题
const currentTitle = computed(() => route.meta?.title as string)

// 切换侧边栏
const toggleSidebar = () => {
  isCollapse.value = !isCollapse.value
}

// 处理用户下拉菜单命令
const handleCommand = async (command: string) => {
  switch (command) {
    case 'profile':
      router.push('/admin/profile')
      break
    case 'logout':
      try {
        await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        
        await authStore.logout()
        router.push('/admin/login')
      } catch {
        // 用户取消
      }
      break
  }
}

onMounted(() => {
  // 从localStorage恢复侧边栏状态
  const savedCollapse = localStorage.getItem('admin_sidebar_collapse')
  if (savedCollapse) {
    isCollapse.value = JSON.parse(savedCollapse)
  }
})

// 监听侧边栏状态变化并保存
watch(() => isCollapse.value, (newVal) => {
  localStorage.setItem('admin_sidebar_collapse', JSON.stringify(newVal))
})
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  background-color: #304156;
  box-shadow: 2px 0 6px rgba(0, 21, 41, 0.35);
  transition: width 0.3s;
  overflow: hidden;
}

.sidebar-header {
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-bottom: 1px solid #434a50;
  padding: 0 20px;
}

.logo {
  text-align: center;
  white-space: nowrap;
}

.logo-mini {
  text-align: center;
}

.sidebar-menu-wrapper {
  height: calc(100vh - 60px);
}

.el-menu {
  border-right: none;
}

.navbar {
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: 60px !important;
}

.navbar-left {
  display: flex;
  align-items: center;
  gap: 20px;
}

.collapse-btn {
  font-size: 18px;
  color: #5a5e66;
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 20px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 8px 12px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: #f5f7fa;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main-content {
  background-color: #f0f2f5;
  padding: 20px;
  overflow-y: auto;
}

.content-wrapper {
  background: #fff;
  border-radius: 4px;
  padding: 24px;
  min-height: calc(100vh - 140px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .navbar {
    padding: 0 15px;
  }
  
  .main-content {
    padding: 15px;
  }
  
  .content-wrapper {
    padding: 16px;
  }
}
</style> 